package org.lmj;


import org.lmj.client.AsyncRpcClient;
import org.lmj.client.HelloService;
import org.lmj.client.IAsyncObjectProxy;
import org.lmj.client.RPCFuture;
import org.lmj.registry.ServiceDiscovery;

import java.util.concurrent.TimeUnit;

/**
 * Created by luxiaoxun on 2016/3/16.
 */
public class BenchmarkAsync {
    public static void main(String[] args) throws InterruptedException {
        ServiceDiscovery serviceDiscovery=new ServiceDiscovery("192.168.0.200:2181");
        final AsyncRpcClient rpcClient=new AsyncRpcClient(String.valueOf(serviceDiscovery));

        int threadNum = 10;
        final int requestNum = 20;
        Thread[] threads = new Thread[threadNum];

        long startTime = System.currentTimeMillis();
        //benchmark for async call
       // for (int i = 0; i < threadNum; ++i) {
           // threads[i] = new Thread(new Runnable() {
        for (int i=0;i<=1;i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    // for (int i = 0; i < requestNum; i++) {
                    try {
                        IAsyncObjectProxy client = rpcClient.createAsync(HelloService.class);
                        RPCFuture helloFuture = client.call("hello", "ok");//Integer.toString(i)
                        String result = (String) helloFuture.get(3000, TimeUnit.MILLISECONDS);
                        System.out.println(result);
//                            if (!result.equals("Hello! " + i))
//                                System.out.println("error = " + result);
                    } catch (Exception e) {
                        System.out.println(e);
                    }
                    //}
                }
            }).start();
        }
            //});
            //threads[i].start();
       // }
        for (int i = 0; i < threads.length; i++) {
            threads[i].join();
        }
        long timeCost = (System.currentTimeMillis() - startTime);
        String msg = String.format("Async call total-time-cost:%sms, req/s=%s", timeCost, ((double) (requestNum * threadNum)) / timeCost * 1000);
        System.out.println(msg);

        rpcClient.stop();

    }
}
